﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.22
 * 描 述：供应商
 */
var refreshGirdData;

var acceptClick;
var dialogClose;

var keyword = request("keyword");
var selectValue = request('selectValue');
var dfopid = request('dfopid');

var bootstrap = function ($, learun) {
    "use strict";
    var categoryId = '';
    var page = {
        init: function () {
            page.initTree();
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({ keyword: keyword });
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword:keyword});
                }
            });
            // 新增
            $('#lr_add').on('click', function () {
                if (!categoryId) {
                    learun.alert.warning('请选择供应商分类！');
                    return false;
                }
                top.selectedDataItemRow = null;
                var parentId = $('#gridtable').jfGridValue('id') || '0';
                learun.layerForm({
                    id: 'form',
                    title: '添加供应商',
                    url: config.webportal_server + 'pages/base/supplier/form.html?access_token=' + config.getToken().access_token + '&category=' + categoryId,
                    width: 800,
                    height: 480,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGird);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                top.selectedDataItemRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '编辑供应商',
                        url: config.webportal_server + 'pages/base/supplier/form.html?access_token=' + config.getToken().access_token + '&category=' + categoryId,
                        width: 800,
                        height: 480,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGird);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'basesuppliers/delete/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGird();
                            });
                        }
                    });
                }
            });
            /*分类管理*/
            $('#lr_category').on('click', function () {
                learun.layerForm({
                    id: 'ClassifyIndex',
                    title: '分类管理',
                    url: config.webportal_server + 'pages/base/supplier/classifyIndex.html?access_token=' + config.getToken().access_token,
                    width: 800,
                    height: 500,
                    maxmin: true,
                    btn: null,
                    end: function () {
                        //location.reload();
                    }
                });
            });
        },
        initTree: function () {
            $('#lr_left_tree').lrtree({
                url: config.base_server() + 'basesuppliercategorys/tree?access_token=' + config.getToken().access_token,
                nodeClick: function (item) {
                    categoryId = item.value;
                    $('#titleinfo').text(item.text);
                    page.search();
                }
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'basesuppliers/list?access_token=' + config.getToken().access_token,
                headData: [
                    { label: '供应商编号', name: 'code', width: 90, align: 'left' },
                    { label: '供应商名称', name: 'name', width: 200, align: 'left' },
                    { label: '联系人', name: 'contactPerson', width: 90, align: 'left' },
                    { label: '联系电话', name: 'contactPhone', width: 150, align: 'left' },
                    { label: '开户行', name: 'bankName', width: 120, align: 'left' },
                    { label: '开户行号', name: 'bankNo', width: 90, align: 'left' },
                    { label: '银行账户', name: 'bankAccount', width: 150, align: 'left' },
                    { label: '纳税识别号', name: 'tin', width: 150, align: 'left' },
                    { label: '地址', name: 'contactAddress', width: 250, align: 'left' },
                    { label: '排序', name: 'sort', width: 80, align: 'center' },
                    {
                        label: "有效", name: "status", width: 50, align: "center",
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                        }
                    },
                    { label: "备注", name: "description", width: 200, align: "left" }
                ],
                dblclick: function (row) {
                    var keyValue = $('#gridtable').jfGridValue('id');
                    top.selectedDataItemRow = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'form',
                            title: '编辑供应商',
                            url: config.webportal_server + 'pages/base/supplier/form.html?access_token=' + config.getToken().access_token + '&category=' + categoryId,
                            width: 800,
                            height: 480,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGird);
                            }
                        });
                    }
                },
                isPage: true,
                isMultiselect: true,
                reloadSelected: true,
                mainId: 'id'
            });
        },
        search: function (param) {
            param = param || {};
            param.category = categoryId;
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    refreshGirdData = function () {
        page.search();
    };

    //确认按钮
    acceptClick = function (callBack) {
        var value = $('#gridtable').jfGridValue('id');
        var selectDatas = $('#gridtable').jfGridGet('rowdata');
        if (learun.checkrow(value)) {
            var origArr = selectValue.split(',');
            var newArr = value.split(',');
            var origStr = origArr.sort().join();
            var newStr = newArr.sort().join();
            if(origStr != newStr) {
                callBack(selectDatas, dfopid);
            }
            return true;
        } else {
            return false;
        }
    }

    dialogClose = function () {
        try {
            var index = top.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            var $IsdialogClose = top.$("#layui-layer" + index).find('.layui-layer-btn').find("#IsdialogClose");
            var IsClose = $IsdialogClose.is(":checked");
            if ($IsdialogClose.length == 0) {
                IsClose = true;
            }
            if (IsClose) {
                top.layer.close(index);
            } else {
                location.reload();
            }
        } catch (e) {
            alert(e);
        }
    }

    page.init();
}


    
